package com.haiyou.dc.thread.test2;

/**
 * 死锁产生：一般发生同步持有多个的对象锁
 */
public class DeadLock {


    public static void main(String[] args) {
        Markup m1 = new Markup(1, "张柏芝");
        new Thread(m1).start();
        Markup m2 = new Markup(2, "王菲");
        new Thread(m2).start();
    }

}

/**
 * 照镜子
 */
class Mirror {

}

/**
 * 涂口红
 */
class Lipstick {

}

/**
 * 化妆
 */
class Markup implements Runnable {

    private static Mirror mirror = new Mirror();
    private static Lipstick lipstick = new Lipstick();

    int choose;
    String girl;

    public Markup(int choose, String girl) {
        this.choose = choose;
        this.girl = girl;
    }

    @Override
    public void run() {
        this.mark();
    }

    public void mark() {
        if (choose == 1) {
            synchronized (mirror) {
                System.out.println(this.girl + "照镜子");
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
//                synchronized (lipstick) {
//                    System.out.println(this.girl + "涂口红");
//                }
            }
            synchronized (lipstick) {
                System.out.println(this.girl + "涂口红");
            }
        } else {
            synchronized (lipstick) {
                System.out.println(this.girl + "涂口红");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
//                synchronized (mirror) {
//                    System.out.println(this.girl + "照镜子");
//                }
            }
            synchronized (mirror) {
                System.out.println(this.girl + "照镜子");
            }
        }
    }
}


